import { _decorator, Camera, Component, error, instantiate, Mat4, Node, Prefab, tween, UITransform, v3, Vec2, Vec3 } from 'cc';
import { AWARYTYPE } from '../../AshFramework/Datas/GameConstant';
import { ZTool } from '../../AshFramework/Utils/ZTool';
const { ccclass, property } = _decorator;

@ccclass('AwardController')
export class AwardController extends Component {

    @property(Prefab)
    GoldPrefab: Prefab = null;

    @property(Prefab)
    MasonryPrefab: Prefab = null;

    deviation: number = 100;//偏差
    UICamera: Camera = null;
    Camera: Camera = null;

    private _UITransform: UITransform = null;
    private _targetPos: Vec3 = null;

    private _arrNode: Array<Node> = new Array();
    private _isLast: boolean = false;
    private _cb: Function = null;

    init(type: AWARYTYPE, targetPos: Vec3, cb: Function = null) {
        let prefab: Prefab = null;
        switch (type) {
            case AWARYTYPE.GOLD:
                prefab = this.GoldPrefab;
                break;
            case AWARYTYPE.MASONRY:
                prefab = this.MasonryPrefab;
                break;
        }
        if (!prefab) {
            console.error(`没找到该奖励！`);
            return;
        }
        this.createAward(prefab);
        this.move(targetPos);
        this._cb = cb;
    }

    initGold(targetPos: Vec3, cb: Function = null) {
        let node: Node = instantiate(this.GoldPrefab);
        this.node.addChild(node);
        node.position = Vec3.ZERO;
        this._UITransform = this.getComponent(UITransform);
        // if (this._UITransform) {
        //     this._targetPos = this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(targetPos);
        // }
        this._targetPos = this.convertCoordinates(targetPos);
        tween(this.node)
            .delay(2)
            .to(0.4, { worldPosition: this._targetPos })
            .call(() => {
                this.node.destroy();
            })
            .start();
        this._cb = cb;
    }

    convertCoordinates(targetPos: Vec3) {
        this.UICamera = this.node.parent.getChildByName("UICamera").getComponent(Camera);
        this.Camera = this.node.parent.getChildByName("Camera").getComponent(Camera);

        // 获取 targetNode 在UI摄像机下的屏幕坐标
        const screenUI = this.UICamera.worldToScreen(targetPos);

        //将屏幕坐标转化为Camera摄像机下的世界坐标
        const worldPos = this.Camera.screenToWorld(screenUI);;
        return worldPos;

        // // 将世界坐标转换为 camera2 的本地坐标
        // const invCamera2ViewMatrix = new Mat4();
        // Mat4.invert(invCamera2ViewMatrix, this.Camera.node.getWorldMatrix());
        // const localPositionInCamera2 = new Vec3();
        // Vec3.transformMat4(localPositionInCamera2, worldPosition, invCamera2ViewMatrix);

        // // 将本地坐标转换为屏幕坐标
        // const screenPositionInCamera2 = new Vec3();
        // this.Camera.worldToScreen(localPositionInCamera2, screenPositionInCamera2);

        // // 转换为 Vec2 类型用于屏幕坐标表示
        // // const screenPositionVec2 = new Vec2(screenPositionInCamera2.x, screenPositionInCamera2.y);

        // return screenPositionInCamera2;
    }

    createAward(prefab: Prefab) {
        for (let index = 0; index < 10; index++) {
            let node: Node = instantiate(prefab);
            this.node.addChild(node);
            let X = ZTool.GetRandom(-this.deviation, this.deviation);
            let Y = ZTool.GetRandom(-this.deviation, this.deviation);
            node.position = v3(X, Y);
            this._arrNode.push(node);
        }
    }

    move(targetPos: Vec3) {
        this._UITransform = this.getComponent(UITransform);
        if (this._UITransform) {
            this._targetPos = this._UITransform.convertToNodeSpaceAR(targetPos);
        }
        let randPos: Vec3 = v3(ZTool.GetRandom(-50, 50), ZTool.GetRandom(-50, 50));
        for (let index = 0; index < this._arrNode.length; index++) {
            if (index == this._arrNode.length - 1) {
                tween(this._arrNode[index])
                    .by(0.2, { position: randPos })
                    .to(0.4, { position: this._targetPos })
                    .call(() => {
                        this.node.destroy();
                    })
                    .start();
            } else {
                tween(this._arrNode[index])
                    .by(0.2, { position: randPos })
                    .to(0.4, { position: this._targetPos })
                    .start();
            }
        }
    }

    protected onDisable(): void {
        this._cb && this._cb();
    }

}


